function initUpl(uploadUrl) {
  var ss = '<form id="fm_upload" action="'+(uploadUrl||'upload')+'" method="post" enctype="multipart/form-data" target="ifm_upload">'
      + '<div id="div_upload" style="position:absolute; display:none; width:60px; height:30px; overflow:hidden; z-index:1000; opacity:0.01; filter:alpha(opacity=1);">'
      + '<input type="file" name="file" id="ip_file" onchange="chkupload();" style="font-size:200px; width:500px; height:500px;" /></div><input type="hidden" name="tp" id="ip_fftp"/></form>'
      + '<iframe name="ifm_upload" onload="uploadOk(this);" style="width:0px; height:0px; display:none;"></iframe>'
      + '<div id="div_wait" style="position:absolute; display:none; z-index:100; border:1px solid #999; background-color:#fffff0; padding:10px 10px;"><span class="loading1">正在上传，请稍等。。。</span></div>';
  document.write(ss);
}

var _fno = 1;
function setupl(e, t) {
  var div = document.getElementById("div_upload");
  if(div) {
    div.style.left = (getElleft(e))+"px";
    div.style.top = (getEltop(e))+"px";
    div.style.width = e.offsetWidth+"px";
    div.style.height = e.offsetHeight+"px";
    div.style.display = "";
    _fno = t||1;
    var itp = document.getElementById("ip_fftp");
    if(itp) itp.value = _fno;
  }
  div = document.getElementById("div_wait");
  if(div) {
    div.style.left = (getElleft(e)+0)+"px";
    div.style.top = (getEltop(e)-10)+"px";
  }
}

function getElleft(el) {
  var et = 0;
  while(el) {
    et += el.offsetLeft;
    el = el.offsetParent;
  }
  return et;
}
function getEltop(el) {
  var et = 0;
  while(el) {
    et += el.offsetTop;
    el = el.offsetParent;
  }
  return et;
}

function clup() {
  var div = document.getElementById("div_upload");
  if(div) div.style.display = "none";
  shpw(false);
}
function shpw(b) {
  var div = document.getElementById("div_wait");
  if(div) div.style.display = b?"":"none";
}

var _isupl = false;
function chkupload() {
  var ff = document.getElementById("ip_file").value;
  if(ff!="") {
    if(ff.length>200) {
      window.alert("文件名超长..");
    } else {
      var ext = ff.substr(ff.length-4).toLowerCase();
      //if(ext==".gif" || ext==".jpg" || ext==".png") {
        document.getElementById("fm_upload").submit();
        _isupl = true;
        shpw(true);
      //} else
      //  window.alert("只能上传gif,jpg和png格式");
    }
  } else {
    window.alert("请选择文件");
  }
}

function uploadOk(ifm) {
  if(!_isupl) return;
  var ret = null;
  var vv = ifm.contentWindow.document.body.innerHTML;
  //alert(vv);
  shpw(false);
  _isupl = false;
  if(vv!="" && vv.length<500000) {
    vv = vv.replace("<PRE","<pre");
    vv = vv.replace("PRE>","pre>");
    n = vv.indexOf("<pre");
    if(n>=0) {
      n = vv.indexOf(">", n+1);
      vv = vv.substr(n+1);
    }
    vv = vv.replace("</pre>","");
    try {
      eval("ret = "+vv);
    } catch(e) {}
  }
  if(ret) {
    if(ret.ret) {
      document.getElementById("fm_upload").reset();
      clup();
      if(window.uploadSucc) uploadSucc(ret.data);
    } else {
      window.alert(ret.msg);
    }
  } else {
    window.alert("上传发生错误..");
    if(vv.length<1000) window.alert(vv);
  }
}

function uploadRet(ret, url) {
  if(ret) {
    document.getElementById("fm_upload").reset();
    if(window.uploadSucc) uploadSucc(url);
  }
  clup();
}
